package com.kbbywyclient.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.Question;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.CommonQuestionModule;
import com.kbbywyclient.presenter.CommonQuestionPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.adapter.base.BaseAdapter;
import com.kbbywyclient.ui.contarct.CommonQuestionContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.divider.VerticalItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by Jiangzx on 2017/9/6 15:44.
 */

public class CommonQuestionActivity extends BaseActivity implements CommonQuestionContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.sr_layout)
    SmartRefreshLayout srLayout;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;

    private BaseAdapter<Question> adapter;

    @Inject
    CommonQuestionPresenter presenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_common_question;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->finish());
        srLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                presenter.mPage++;
                presenter.loadData(presenter.mPage);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                presenter.clearList();
                setEmptyList();
                presenter.loadData(presenter.mPage);
            }
        });

        LinearLayoutManager layoutManager=new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        VerticalItemDecoration decoration=new VerticalItemDecoration(Constants.DIVIDER_COLOR,1);
        recyclerView.addItemDecoration(decoration);
    }

    @Override
    public void initData() {
        srLayout.autoRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("常见问题");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getCommonQuestionComponent(new CommonQuestionModule(this)).inject(this);
    }


    @Override
    public void loadDataSuccess(List<Question> data, boolean canLoadMore) {
        if(adapter==null){
            adapter=new BaseAdapter<Question>(data,R.layout.item_common_question) {
                @Override
                public void convert(Holder holder, Question item, int position) {
                    holder.setText(R.id.tv_text,item.getTitle());
                    holder.itemView.setOnClickListener(v->{
                        HashMap<String,Object> webParams=new HashMap<>();
                        webParams.put(WebViewActivity.KEY_URL_S,
                                API.QUESTION_DETAIL+item.getQid());
                        webParams.put(WebViewActivity.KEY_TITLE_S,
                                "问题详情");
                        webParams.put(WebViewActivity.KEY_SCREEN_NAME_S,"问题详情");
                        ActivitySkipUtil.skipAnotherActivity(CommonQuestionActivity.this,
                                WebViewActivity.class,webParams);
                    });
                }
            };
        }
        if(recyclerView.getAdapter()==null){
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
        srLayout.setLoadmoreFinished(!canLoadMore);
    }

    @Override
    public void setEmptyList() {
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void loadDataComplete() {
        srLayout.finishRefresh();
        srLayout.finishLoadmore();
    }
}
